package com.mego.tzadd.activitys;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mego.tzadd.R;
import com.mego.tzadd.utils.HttpRequest;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.base.BaseView;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.model.Entry;
import zuo.biao.library.util.JSON;

//常见问题
public class CjwtActivity extends BaseActivity {

    private final String TAG = "CjwtActivity";

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    public static Intent createIntent(Context context) {
        return new Intent(context, CjwtActivity.class);
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cjwt_activity);

        initView();
        initData();
        initEvent();

    }

    private ListView lv_cjwt;

    @Override
    public void initView() {//必须在onCreateView方法内调用
        ImageButton ivBaseTabReturn = (ImageButton) findViewById(R.id.ivBaseTabReturn);
        if (ivBaseTabReturn != null) {
            ivBaseTabReturn.setScaleType(ImageView.ScaleType.FIT_START);
        }

        lv_cjwt = (ListView) findViewById(R.id.lv_cjwt);
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<Integer, String>> list) {
        if (list == null) {
            return;
        }
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_cjwt.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<Integer, String>> czjllist;
    private ArrayList<Cjwt> mCjwtList = new ArrayList<Cjwt>();

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        mCjwtList.clear();

        try {
            HttpRequest.cjwt(0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    if (code == 1) {
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject a = jsonArray.getJSONObject(i);
                            Cjwt czjl = new Cjwt();
                            czjl.name = a.getString("name");
                            czjl.wenti = a.getString("wenti");
                            czjl.huifu = a.getString("huifu");
                            mCjwtList.add(czjl);
                        }
                        czjllist = getList();
                        setList(czjllist);
                    } else {
                        showShortToast("获取机台列表失败！");
                    }
                    dismissProgressDialog();
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
        }
    }

    protected List<Entry<Integer, String>> getList() {
        czjllist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < mCjwtList.size(); i++) {
            czjllist.add(new Entry<Integer, String>(i, ""));
        }
        return czjllist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用

    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    @Override
    public void onForwardClick(View v) {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public class DemoView extends BaseView<Entry<Integer, String>> implements View.OnClickListener {
        private static final String TAG = "DemoView";

        public DemoView(Activity context, ViewGroup parent) {
            super(context, R.layout.wt_view, parent);  //TODO demo_view改为你所需要的layout文件
        }

        //示例代码<<<<<<<<<<<<<<<<
        public TextView tv_index;
        public TextView tv_wtt;
        public TextView tv_wtc;

        //示例代码>>>>>>>>>>>>>>>>
        @Override
        public View createView() {

            //示例代码<<<<<<<<<<<<<<<<
            tv_index = findView(R.id.tv_index);
            tv_wtt = findView(R.id.tv_wtt);
            tv_wtc = findView(R.id.tv_wtc);
            //示例代码>>>>>>>>>>>>>>>>

            return super.createView();
        }


        @Override
        public void bindView(Entry<Integer, String> data_) {
            //示例代码<<<<<<<<<<<<<<<<
            super.bindView(data_ != null ? data_ : new Entry<Integer, String>());
            int index = data.getKey();
            if (index >= 0 && index < mCjwtList.size()) {
                Cjwt a = mCjwtList.get(index);
                tv_index.setText(new DecimalFormat("00").format(index + 1));
                tv_wtt.setText(a.name);
                tv_wtc.setText(Html.fromHtml(a.wenti));
            }
            //示例代码>>>>>>>>>>>>>>>>
        }


        //示例代码<<<<<<<<<<<<<<<<
        @Override
        public void onClick(View v) {
            if (data == null) {
                return;
            }
            int index = data.getKey();
            Cjwt a = mCjwtList.get(index);
            switch (v.getId()) {
                default:
                    break;
            }
        }
        //示例代码>>>>>>>>>>>>>>>>

    }

    public class DemoAdapter extends BaseAdapter<Entry<Integer, String>, DemoView> {

        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public DemoView createView(int position, ViewGroup parent) {
            return new DemoView(context, parent);
        }

        @Override
        public long getItemId(int position) {
            return getItem(position).getId();
        }


    }

    class Cjwt {
        public String name = "";
        public String wenti = "";
        public String huifu = "";
    }

}

